using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spread.Common.Features.Sample;
using FarPoint.Win.Spread.Model;

namespace Spread.Worksheet.Features
{
    public partial class MergeAndSpanDemo : BaseSampleControl
    {
        public MergeAndSpanDemo()
        {
            InitializeComponent();
            Name = "Cell Merging and Spanning";
            
            //Spanning Header
            fpSpread1_Sheet1.ColumnHeader.RowCount = 2;
            fpSpread1_Sheet1.ColumnHeader.SetClip(0, 0, 2, 7, "ID\tName\t\tDepartment\tEmail\tAddress\t\n" +
                "\tFirst Name\tLast Name\t\t\tCurrent\tPermanent\n");
            fpSpread1_Sheet1.ColumnHeader.Cells[0, 0].RowSpan = 2;
            fpSpread1_Sheet1.ColumnHeader.Cells[0, 1].ColumnSpan = 2;
            fpSpread1_Sheet1.ColumnHeader.Cells[0, 3].RowSpan = 2;
            fpSpread1_Sheet1.ColumnHeader.Cells[0, 4].RowSpan = 2;
            fpSpread1_Sheet1.ColumnHeader.Cells[0, 5].ColumnSpan = 2;
            
            object[,] data = new object[,]
            {
                {1, "John", "Smith", "IT", "John.Smith@gmail.com", "Athens Place,Washington", "Athens Place,Washington"},
                {2, "Oliver", "Williams", "IT", "Oliver.Williams@gmail.com", "Quebec Place,Washington", "Quebec Place,Washington"},
                {3, "Robert", "Taylor", "IT", "Robert.Taylor@gmail.com", "Peshwar Place,Washington", "Peshwar Place,Washington"},
                {4, "Ellen", "Thomas", "IT", "Ellen.Thomas@gmail.com", "Quebec Place,Washington", "Peshwar Place,Washington"},
                {5, "Jane", "Roberts", "Development", "Jane.Roberts@gmail.com", "Pennsylvania", "Maryland"},
                {6, "Charles", "White", "Development", "Charles.White@gmail.com", "Maryland", "Virginia"},
                {7, "Emma", "Jackson", "Development", "Emma.Jackson@gmail.com", "Pennsylvania", "Delaware"},
                {8, "Albert", "Green", "Development", "Albert.Green@gmail.com", "Maryland", "Washington"},
                {9, "Michael", "Robinson", "Development", "Michael.Robinson@gmail.com", "Virginia", "Virginia"},
                {10, "Leo", "Brown", "Development", "Leo.Brown@gmail.com", "Washington", "Pennsylvania"},
                {11, "Archie", "Wood", "Development", "Archie.Wood@gmail.com", "Virginia", "Maryland"},
                {12, "Henry", "Turner", "Development", "Henry.Turner@gmail.com", "Delaware", "Delaware"},
                {13, "Susan", "Taylor", "Marketing", "Susan.Taylor@gmail.com", "Pennsylvania", "Virginia"},
                {14, "Florence", "Green", "Marketing", "Florence.Green@gmail.com", "Delaware", "Delaware"},
                {15, "Sophia", "Lewis", "Marketing", "Sophia.Lewis@gmail.com", "Maryland", "Washington"},
                {16, "George", "Harris", "Marketing", "George.Harris@gmail.com", "Delaware", "Pennsylvania"},
                {17, "Jacob", "Smith", "Marketing", "Jacob.Smith@gmail.com", "Pennsylvania", "Virginia"},
                {18, "Ruby", "Jones", "Marketing", "Ruby.Jones@gmail.com", "Maryland", "Washington"},
                {19, "Thomas", "Evans", "Customer Support", "Thomas.Evans@gmail.com", "Delaware", "Delaware"},
                {20, "Jessica", "Robinson", "Customer Support", "Jessica.Robinson@gmail.com", "Washington", "Maryland"},
                {21, "Harry", "Brown", "Customer Support", "Harry.Brown@gmail.com", "Washington", "Washington"},
                {22, "Joseph", "Hall", "Customer Support", "Joseph.Hall@gmail.com", "Delaware", "Delaware"},
                {23, "Noah", "Walker", "Sales", "Noah.Walker@gmail.com", "Pennsylvania", "Washington"},
                {24, "Edward", "Smith", "Sales", "Edward.Smith@gmail.com", "Maryland", "Delaware"},
                {25, "Ella", "Thomas", "Sales", "Ella.Thomas@gmail.com", "Virginia", "Virginia"},
            };

            for (int r = 0; r <= data.GetUpperBound(0); r++)
            {
                for (int c = 0; c <= data.GetUpperBound(1); c++)
                {
                    fpSpread1_Sheet1.Cells[r, c].Value = data[r, c];
                }
            }

            fpSpread1_Sheet1.SetColumnWidth(-1, 100);
            fpSpread1_Sheet1.SetRowHeight(-1, 21);
            fpSpread1_Sheet1.Columns[0].Width = 50;
            fpSpread1_Sheet1.SetColumnMerge(-1, MergePolicy.Always);
        }

        private void RadioButton_CheckedChanged(object sender, EventArgs e)
        {
            var rb = sender as RadioButton;
            if (rb.Checked)
            {
                switch (rb.Text)
                {
                    case "None":
                        fpSpread1_Sheet1.SetColumnMerge(-1, MergePolicy.None);
                        fpSpread1_Sheet1.SetRowMerge(-1, MergePolicy.None);
                        break;
                    case "Row":
                        fpSpread1_Sheet1.SetColumnMerge(-1, MergePolicy.None);
                        fpSpread1_Sheet1.SetRowMerge(-1, MergePolicy.Always);
                        break;
                    case "Column":
                        fpSpread1_Sheet1.SetColumnMerge(-1, MergePolicy.Always);
                        fpSpread1_Sheet1.SetRowMerge(-1, MergePolicy.None);
                        break;
                    case "Both":
                        fpSpread1_Sheet1.SetColumnMerge(-1, MergePolicy.Always);
                        fpSpread1_Sheet1.SetRowMerge(-1, MergePolicy.Always);
                        break;
                }
            }
        }

        private void _bSpan_Click(object sender, EventArgs e)
        {
            CellRange cr = fpSpread1_Sheet1.GetSelection(0);
            if (cr != null)
            {
                if (cr.ColumnCount > 1 || cr.RowCount > 1)
                    fpSpread1_Sheet1.AddSpanCell(cr.Row, cr.Column, cr.RowCount, cr.ColumnCount);
            }
        }

        private void _bURemoveSpan_Click(object sender, EventArgs e)
        {
            CellRange cr = fpSpread1_Sheet1.GetSelection(0);
            if (cr != null)
            {
                if(cr.Row!=-1 && cr.Column!=-1)
                    fpSpread1_Sheet1.RemoveSpanCell(cr.Row, cr.Column);
            }
        }

        private void _cbAllowOverFlow_CheckedChanged(object sender, EventArgs e)
        {
            fpSpread1.AllowCellOverflow = _cbAllowOverFlow.Checked;
        }

        private void _cbEditOverflow_CheckedChanged(object sender, EventArgs e)
        {
            fpSpread1.AllowEditOverflow = _cbEditOverflow.Checked;
        }
    }    
}


//
//MergeAndSpanDemo.Designer.cs
//


namespace Spread.Worksheet.Features
{
    partial class MergeAndSpanDemo
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread1 = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread1_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this._rbMergeCol = new System.Windows.Forms.RadioButton();
            this._rbMergeRow = new System.Windows.Forms.RadioButton();
            this._rbMergeNone = new System.Windows.Forms.RadioButton();
            this._bSpan = new System.Windows.Forms.Button();
            this._bRemoveSpan = new System.Windows.Forms.Button();
            this._cbAllowOverFlow = new System.Windows.Forms.CheckBox();
            this._cbEditOverflow = new System.Windows.Forms.CheckBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this._rbMergeBoth = new System.Windows.Forms.RadioButton();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1_Sheet1)).BeginInit();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread1);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.groupBox3);
            this.pnlControls.Controls.Add(this.groupBox2);
            this.pnlControls.Controls.Add(this.groupBox1);
            // 
            // fpSpread1
            // 
            this.fpSpread1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread1.Location = new System.Drawing.Point(0, 0);
            this.fpSpread1.Name = "fpSpread1";
            this.fpSpread1.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread1_Sheet1});
            this.fpSpread1.Size = new System.Drawing.Size(477, 487);
            this.fpSpread1.TabIndex = 0;
            // 
            // fpSpread1_Sheet1
            // 
            this.fpSpread1_Sheet1.Reset();
            this.fpSpread1_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread1_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread1_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // groupBox1
            // 
            this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox1.Controls.Add(this._rbMergeBoth);
            this.groupBox1.Controls.Add(this._rbMergeCol);
            this.groupBox1.Controls.Add(this._rbMergeRow);
            this.groupBox1.Controls.Add(this._rbMergeNone);
            this.groupBox1.Location = new System.Drawing.Point(6, 20);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(140, 124);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Merging Options";
            // 
            // _rbMergeCol
            // 
            this._rbMergeCol.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._rbMergeCol.AutoSize = true;
            this._rbMergeCol.Checked = true;
            this._rbMergeCol.Location = new System.Drawing.Point(7, 66);
            this._rbMergeCol.Name = "_rbMergeCol";
            this._rbMergeCol.Size = new System.Drawing.Size(60, 17);
            this._rbMergeCol.TabIndex = 2;
            this._rbMergeCol.TabStop = true;
            this._rbMergeCol.Text = "Column";
            this._rbMergeCol.UseVisualStyleBackColor = true;
            this._rbMergeCol.CheckedChanged += new System.EventHandler(this.RadioButton_CheckedChanged);
            // 
            // _rbMergeRow
            // 
            this._rbMergeRow.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._rbMergeRow.AutoSize = true;
            this._rbMergeRow.Location = new System.Drawing.Point(7, 43);
            this._rbMergeRow.Name = "_rbMergeRow";
            this._rbMergeRow.Size = new System.Drawing.Size(47, 17);
            this._rbMergeRow.TabIndex = 1;
            this._rbMergeRow.Text = "Row";
            this._rbMergeRow.UseVisualStyleBackColor = true;
            this._rbMergeRow.CheckedChanged += new System.EventHandler(this.RadioButton_CheckedChanged);
            // 
            // _rbMergeNone
            // 
            this._rbMergeNone.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._rbMergeNone.AutoSize = true;
            this._rbMergeNone.Location = new System.Drawing.Point(7, 20);
            this._rbMergeNone.Name = "_rbMergeNone";
            this._rbMergeNone.Size = new System.Drawing.Size(51, 17);
            this._rbMergeNone.TabIndex = 0;
            this._rbMergeNone.Text = "None";
            this._rbMergeNone.UseVisualStyleBackColor = true;
            this._rbMergeNone.CheckedChanged += new System.EventHandler(this.RadioButton_CheckedChanged);
            // 
            // _bSpan
            // 
            this._bSpan.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._bSpan.Location = new System.Drawing.Point(8, 28);
            this._bSpan.Name = "_bSpan";
            this._bSpan.Size = new System.Drawing.Size(118, 23);
            this._bSpan.TabIndex = 1;
            this._bSpan.Text = "Add Span";
            this._bSpan.UseVisualStyleBackColor = true;
            this._bSpan.Click += new System.EventHandler(this._bSpan_Click);
            // 
            // _bRemoveSpan
            // 
            this._bRemoveSpan.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._bRemoveSpan.Location = new System.Drawing.Point(8, 72);
            this._bRemoveSpan.Name = "_bRemoveSpan";
            this._bRemoveSpan.Size = new System.Drawing.Size(119, 23);
            this._bRemoveSpan.TabIndex = 2;
            this._bRemoveSpan.Text = "Remove Span";
            this._bRemoveSpan.UseVisualStyleBackColor = true;
            this._bRemoveSpan.Click += new System.EventHandler(this._bURemoveSpan_Click);
            // 
            // _cbAllowOverFlow
            // 
            this._cbAllowOverFlow.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._cbAllowOverFlow.AutoSize = true;
            this._cbAllowOverFlow.Location = new System.Drawing.Point(8, 28);
            this._cbAllowOverFlow.Name = "_cbAllowOverFlow";
            this._cbAllowOverFlow.Size = new System.Drawing.Size(119, 17);
            this._cbAllowOverFlow.TabIndex = 3;
            this._cbAllowOverFlow.Text = "Allow Cell OverFlow";
            this._cbAllowOverFlow.UseVisualStyleBackColor = true;
            this._cbAllowOverFlow.CheckedChanged += new System.EventHandler(this._cbAllowOverFlow_CheckedChanged);
            // 
            // _cbEditOverflow
            // 
            this._cbEditOverflow.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._cbEditOverflow.AutoSize = true;
            this._cbEditOverflow.Location = new System.Drawing.Point(7, 68);
            this._cbEditOverflow.Name = "_cbEditOverflow";
            this._cbEditOverflow.Size = new System.Drawing.Size(120, 17);
            this._cbEditOverflow.TabIndex = 4;
            this._cbEditOverflow.Text = "Allow Edit OverFlow";
            this._cbEditOverflow.UseVisualStyleBackColor = true;
            this._cbEditOverflow.CheckedChanged += new System.EventHandler(this._cbEditOverflow_CheckedChanged);
            // 
            // groupBox2
            // 
            this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox2.Controls.Add(this._cbEditOverflow);
            this.groupBox2.Controls.Add(this._cbAllowOverFlow);
            this.groupBox2.Location = new System.Drawing.Point(6, 284);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(140, 105);
            this.groupBox2.TabIndex = 5;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Cell OverFlow";
            // 
            // groupBox3
            // 
            this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox3.Controls.Add(this._bSpan);
            this.groupBox3.Controls.Add(this._bRemoveSpan);
            this.groupBox3.Location = new System.Drawing.Point(6, 151);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(140, 118);
            this.groupBox3.TabIndex = 6;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Cell Span";
            // 
            // _rbMergeBoth
            // 
            this._rbMergeBoth.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this._rbMergeBoth.AutoSize = true;
            this._rbMergeBoth.Location = new System.Drawing.Point(7, 89);
            this._rbMergeBoth.Name = "_rbMergeBoth";
            this._rbMergeBoth.Size = new System.Drawing.Size(47, 17);
            this._rbMergeBoth.TabIndex = 3;
            this._rbMergeBoth.TabStop = true;
            this._rbMergeBoth.Text = "Both";
            this._rbMergeBoth.UseVisualStyleBackColor = true;
            this._rbMergeBoth.CheckedChanged += new System.EventHandler(this.RadioButton_CheckedChanged);
            // 
            // MergeAndSpanDemo
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "MergeAndSpanDemo";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1_Sheet1)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread1;
        private FarPoint.Win.Spread.SheetView fpSpread1_Sheet1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.RadioButton _rbMergeCol;
        private System.Windows.Forms.RadioButton _rbMergeRow;
        private System.Windows.Forms.RadioButton _rbMergeNone;
        private System.Windows.Forms.Button _bRemoveSpan;
        private System.Windows.Forms.Button _bSpan;
        private System.Windows.Forms.CheckBox _cbAllowOverFlow;
        private System.Windows.Forms.CheckBox _cbEditOverflow;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.RadioButton _rbMergeBoth;
    }
}
